package com.eatme.web.app.coupon.settlement.executor.impl;

import com.eatme.web.app.coupon.settlement.constant.RuleFlag;
import com.eatme.web.app.coupon.settlement.executor.AbstractExecutor;
import com.eatme.web.app.coupon.settlement.executor.RuleExecutor;
import com.eatme.web.app.coupon.common.vo.CouponTemplateSDK;
import com.eatme.web.app.coupon.common.vo.SettlementInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * 折扣优惠券结算规则执行器
 */
@Component
public class ZheKouExecutor extends AbstractExecutor implements RuleExecutor {

    private static final Logger LOG = LoggerFactory.getLogger(ZheKouExecutor.class);

    /**
     * 规则类型标记
     * @return {@link RuleFlag}
     */
    @Override
    public RuleFlag ruleConfig() {
        return RuleFlag.ZHEKOU;
    }

    /**
     * 优惠券规则的计算
     * @param settlement {@link SettlementInfo} 包含了选择的优惠券
     * @return {@link SettlementInfo} 修正过的结算信息
     */
    @Override
    public SettlementInfo computeRule(SettlementInfo settlement) {

        Integer goodsSum = goodsCostSum(
                settlement.getGoodsInfos()
        );
        SettlementInfo probability = processGoodsTypeNotSatisfy(
                settlement, goodsSum
        );
        if (null != probability) {
            LOG.debug("ZheKou Template Is Not Match GoodsType!");
            return probability;
        }

        // 折扣优惠券可以直接使用, 没有门槛
        CouponTemplateSDK templateSDK = settlement.getCouponAndTemplateInfos()
                .get(0).getTemplate();
        Integer quota = templateSDK.getRule().getDiscount().getQuota();

        // 计算使用优惠券之后的价格
        settlement.setCost(
                retainToInt((goodsSum * (quota * 1.0 / 100))) > minCost() ?
                retainToInt((goodsSum * (quota * 1.0 / 100)))
                : minCost()
        );
        LOG.debug("Use ZheKou Coupon Make Goods Cost From {} To {}",
                goodsSum, settlement.getCost());

        return settlement;
    }
}
